package com.dayuanit.dy15.atm.controller;

import com.dayuanit.dy15.atm.annotation.AutoWired;
import com.dayuanit.dy15.atm.annotation.Component;
import com.dayuanit.dy15.atm.annotation.RequestMapping;
import com.dayuanit.dy15.atm.dto.CardDTO;
import com.dayuanit.dy15.atm.dto.ResponseDTO;
import com.dayuanit.dy15.atm.entity.User;
import com.dayuanit.dy15.atm.exception.BizException;
import com.dayuanit.dy15.atm.service.CardService;
import com.dayuanit.dy15.atm.service.impl.CardServiceImpl;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

@Component(value = "cardController")
public class CardController extends BaseController {

    private CardService cardService;

    @AutoWired("cardServiceImpl")
    public void setCardService(CardService cardService) {
        this.cardService = cardService;
    }

    /**
     * 开户
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/card/openaccount.do")
    public ResponseDTO openaccount(HttpServletRequest request, HttpServletResponse response) {

        String password = request.getParameter("password");
        String confirmPassword = request.getParameter("confirmPassword");

        cardService.openaccount(1000L, password, confirmPassword);
        return ResponseDTO.sucess();
    }

    /**
     * 取款
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/card/draw.do")
    public ResponseDTO draw(HttpServletRequest request, HttpServletResponse response) {
        String cardId = request.getParameter("cardId");
        String password = request.getParameter("password");
        String amount = request.getParameter("amount");

        cardService.withdraw(Long.valueOf(cardId), password, amount);
        return ResponseDTO.sucess();
    }

    /**
     * 存款
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/card/deposit.do")
    public ResponseDTO deposit(HttpServletRequest request, HttpServletResponse response) {
        String cardId = request.getParameter("cardId");
        String password = request.getParameter("password");
        String amount = request.getParameter("amount");

        cardService.deposit(Long.valueOf(cardId), password, amount);

        return ResponseDTO.sucess();
    }

    @RequestMapping(value = "/card/listMyCard.do")
    public ResponseDTO listMyCard(HttpServletRequest request, HttpServletResponse response) {
        List<CardDTO> dtos = cardService.queryCards(getUserId(request));
        return ResponseDTO.sucess(dtos);
    }
}
